package com.vshop.framework.web.handler;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.lock.exception.LockFailureException;
import com.vshop.framework.core.domain.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Redis异常处理器
 */
@Slf4j
@RestControllerAdvice
@Order(1)
public class LockFailureExceptionHandler {

    /**
     * 分布式锁Lock4j异常
     */
    @ExceptionHandler(LockFailureException.class)
    public R<Void> handleLockFailureException(LockFailureException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',获取锁失败了'{}',发生Lock4j异常", requestURI, e.getMessage());
        log.error(ExceptionUtil.stacktraceToString(e));
        return R.fail(HttpStatus.HTTP_UNAVAILABLE, "业务处理中，请稍后再试...");
    }

}
